<?php
function Main($mysql,$namearr,$contentfile,$type='')
{
	echo '========》》》内容写入数据库开始<br/>';
	$handle = @fopen($contentfile, "r");
	if ($handle) 
	{
		while (!feof($handle)) 
		{
			$buffer = fgets($handle, 4096);
			//echo $buffer;
			if($type=='')
			{
				writeSms($mysql,$buffer,$namearr);
			}
			else 
			{
				writeSmsType($mysql,$buffer);
			}
		}
		fclose($handle);
	}
	echo '========》》》内容写入数据库结束<br/>';
}

function writeSmsType($mysql,$smscontent)
{
	$smscontent = trim($smscontent);
	if(!empty($smscontent))
	{
		$data                 = array();
		$data['type_name']    = $smscontent;
		$data['parentid ']    = 0;
		$res = $mysql->insert($data, 'sms_type');
		//var_dump($data);
		//var_dump($res);
	}
}

function writeSms($mysql,$smscontent,$namearr)
{
	$smscontent = trim($smscontent);
	if(!empty($smscontent))
	{
		$data                = array();
//		$smscontent          = str_replace("-","",$smscontent);
//		$smscontent          = str_replace("●","",$smscontent);
//		$smscontent          = str_replace("☆","",$smscontent);
		$data['content']     = $namearr[rand(0,18)] . $smscontent;
		$data['typeid']      = rand(1,16);
		$data['isfavorite']  = 0;
		$res = $mysql->insert($data, 'sms_content');
		//var_dump($data);
		//var_dump($res);
	}
}

function updateSms($mysql)
{
	$id  = rand(212,512);
	$where = 'id='.$id;
	$arr               = array();
	$arr['typeid']     = 0;	
	$arr['isfavorite'] = 2;	
	$res = $mysql->update($arr,'sms_content',$where);
	//var_dump($res);
	//var_dump($res);
}
function truncateSms($mysql)
{
	echo '========》》》清空表中数据开始<br/>';
	echo '========》》》清空表sms_content中数据开始<br/>';
	$sql = "TRUNCATE TABLE `sms_content`";
	$res = $mysql->query($sql);
	echo '========》》》清空表sms_content中数据结束<br/>';
	echo '========》》》清空表sms_type中数据开始<br/>';
	$sql = "TRUNCATE TABLE `sms_type`";
	$res = $mysql->query($sql);	
	echo '========》》》清空表sms_type中数据结束<br/>';
	//var_dump($res);
	//var_dump($res);
	echo '========》》》清空表中数据结束<br/>';
}
function deleteFile($myfile)
{
	echo '========》》》删除文件开始<br/>';

	if (file_exists($myfile)) 
	{
		$result= unlink($myfile);
		//echo $result;
	}
	echo '========》》》删除文件结束<br/>';
}

function writeSql($str,$sqlfile)
{
	$m       = fopen($sqlfile,"a");
	//当参数为"w"时是将内容覆盖写入文件，而当参数为“a”时是将内容追加写入。
	$content = $str;
	fwrite($m,$content);
	fclose($m);
}
function createSql($mysql,$sqlfile)
{
	echo '========》》》创建sql文件开始<br/>';
	//类型
	$query = $mysql->select('*', 'sms_type');
	foreach($query as $v)
	{
		$str = "insert into sms_type(type_name,parentid) values('".$v['type_name']."',0);&";
		writeSql($str,$sqlfile);
	}
	//内容
	$query = $mysql->select('*', 'sms_content');
	foreach($query as $v)
	{
		$str = "insert into sms_content(content,typeid,isfavorite) values('".$v['content']."','".$v['typeid']."','".$v['isfavorite']."');&";
		writeSql($str,$sqlfile);
	}
	echo '========》》》创建sql文件结束<br/>';
}
?>